package www.bancodelanacion.intranet.prestamos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import www.bancodelanacion.intranet.prestamos.entidad.ClienteDTO;
import www.bancodelanacion.intranet.prestamos.util.MySqlDBConn;

public class ClienteDAOImpl implements ClienteDAO {

	@Override
	public int insertaCliente(ClienteDTO bean) throws SQLException {
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="insert into tb_cliente values(null,?,?,?,?,?,?,?)";
				pstm = conn.prepareStatement(sql);
				pstm.setString(1,bean.getCodigocliente());
				pstm.setString(2,bean.getRuc());
				pstm.setString(3,bean.getRazonsocial());
				pstm.setString(4,bean.getDireccion());
				pstm.setString(5,bean.getFax());
				pstm.setString(6,bean.getPaginaweb());
				pstm.setString(7,bean.getCorreo());
				return pstm.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return -1;
	}

	@Override
	public int actualizaCliente(ClienteDTO bean) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int eliminaCliente(Integer idProducto) throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ClienteDTO buscaCliente(Integer idProducto) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<ClienteDTO> listarCliente()throws SQLException  {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		ArrayList<ClienteDTO> data = new ArrayList<ClienteDTO>();
		try {
				conn = new MySqlDBConn().getConnection();
				String sql ="select * from tb_cliente";
				pstm = conn.prepareStatement(sql);
				rs = pstm.executeQuery();
				ClienteDTO dto = null;
				while(rs.next()){
					dto = new ClienteDTO();
					dto.setIdcodigo(rs.getInt("id_cliente"));
					dto.setCodigocliente(rs.getString("Codigo_Cliente"));
					dto.setRazonsocial(rs.getString("Razon_Social"));
					dto.setDireccion(rs.getString("Direccion"));
					dto.setRuc(rs.getString("Ruc"));
					dto.setFax(rs.getString("Fax"));
					dto.setPaginaweb(rs.getString("Pagina_Web"));
					dto.setCorreo(rs.getString("Correo"));
					
					data.add(dto);
				}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!= null)rs.close();
				if(pstm!= null)pstm.close();
				if(conn!= null)conn.close();
			} catch (Exception e2) {}
		}
		return data;
	}
	}
	
	


